package org.theSeed.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.theSeed.base.exp.BuzExp;
import org.theSeed.base.exp.ServiceExp;
import org.theSeed.base.pojo.sysEnum.SysCode;
import org.theSeed.base.pojo.vo.R;

import java.sql.SQLException;

/**
 * 全局异常拦截
 */
@Slf4j
@RestControllerAdvice
public class SeedGlobalControllerAdvice {

    /**
     * 全局处理异常
     */
    @ExceptionHandler(Exception.class)
    public R exception(Exception exp){
        log.error(exp.getMessage());
        return R.fail(SysCode.UNKNOW_FAIL);
    }

    /**
     * 运行处理异常
     */
    @ExceptionHandler(RuntimeException.class)
    public R runtimeException(RuntimeException exp){
        log.error(exp.getMessage());
        return R.fail(SysCode.FAIL);
    }

    /**
     * 认证异常
     */
//    @ExceptionHandler(AuthenticationException.class)
//    public R authenticationException(AuthenticationException exp){
//        log.error(exp.getMessage());
//        return R.fail(SysCode.UN_AUTH);
//    }

    /**
     * 权限不足异常
     */
//    @ExceptionHandler(AccessDeniedException.class)
//    public R accessDeniedException(AccessDeniedException exp){
//        log.error(exp.getMessage());
//        return R.fail(SysCode.UN_PERMISSON);
//    }

    /**
     * 自定义业务异常
     */
    @ExceptionHandler(BuzExp.class)
    public R buzExp(BuzExp exp){
        log.error(exp.getMessage());
        return R.fail(SysCode.FAIL,exp.getMessage());
    }

    /**
     * 服务异常
     */
    @ExceptionHandler(ServiceExp.class)
    public R serviceExp(ServiceExp exp){
        log.error(exp.getMessage());
        return R.fail(SysCode.FAIL,exp.getMessage());
    }

    /**
     * 数据库异常
     */
    @ExceptionHandler(SQLException.class)
    public R sqlException(SQLException exp){
        log.error(exp.getMessage());
        return R.fail(SysCode.FAIL,"数据库执行异常");
    }

    /**
     * 数据库异常
     */
//    @ExceptionHandler(DataAccessException.class)
//    public R dataAccessException(DataAccessException exp){
//        log.error(exp.getMessage());
//        return R.fail(SysCode.FAIL,"数据库执行异常");
//    }

    /**
     * 参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public R illegalArgumentException(IllegalArgumentException exp){
        log.error(exp.getMessage());
        return R.fail(SysCode.PARAM_ERR,exp.getMessage());
    }
}
